mini.parse();

var editorId = "ke";
var data0;

function MessageFormHandler() {
};

MessageFormHandler.instance = new MessageFormHandler();

//获得全局的商品类型数据表格对象
MessageFormHandler.messageGrid = mini.get("messageGrid");

var data = [ 
{
	id : "-1",
	text : "全部"
},
{
	id : "0",
	text : "隐藏"
}, {
	id : "1",
	text : "展示"
} ];

try {
	var bmbState = mini.get("bmbState");
	bmbState.load(data);
	bmbState.setValue("-1");
} catch (e) {
}

MessageFormHandler.isQueryBtnExpended = false;
MessageFormHandler.queryShow = function() {
	if (MessageFormHandler.isQueryBtnExpended) {
		MessageFormHandler.isQueryBtnExpended = false;
		mini.get("member_messages_query").setText("展开查询");
		mini.get("member_messages_query").setIconCls("icon-btn-expand");
	} else {
		MessageFormHandler.isQueryBtnExpended = true;
		mini.get("member_messages_query").setText("收起查询");
		mini.get("member_messages_query").setIconCls("icon-btn-collapse");
	}
	$("#queryMessageDiv").toggle(500);
	setTimeout(function() {
		mini.get("messagePanel").doLayout();
	}, 600);
}

MessageFormHandler.doQuery = function() {
	mini.get("messageGrid").load( {
		bumName : mini.get("bmbNick").getValue(),
		bumTitle : mini.get("bmbTitle").getValue(),
		bumState : mini.get("bmbState").getValue(),
		isreply : $('input[name="isReply"]:checked').val()
	});
}

MessageFormHandler.delMessage = function() {
	var rowsData = mini.get("messageGrid").getSelecteds();
	
	if (rowsData.length > 0) {
		mini.confirm("确定删除选择的"+rowsData.length+"条记录？", "确定？", function(action) {
			if (action == "ok") {
				var ts = "";
				for ( var i = 0; i < rowsData.length; i++){
					ts += "'" + rowsData[i].bumId + "',";
				}
				
				mini.get("messageGrid").loading();
				$.ajax( {
					url : "../../../delMessage.do",
					type : "POST",
					data : {
						bmbIdlist : ts.substring(0, ts.length - 1),
						lslUser: gbOperateUser,
    					lslAddress: gbOperateIp
					},
					cache : false,
					success : function(text) {
						mini.get("messageGrid").unmask();
						MiniuiTools.miniInfo("系统提示","操作成功！", function(){
			        		mini.get("messageGrid").reload();
			        	});
					},
					error : function(jqXHR, textStatus, errorThrown) {
						mini.alert(jqXHR.responseText, "系统提示", null);
				}
				});
			}
		});
	} else {
		mini.alert("请选择要编辑的资源！", "系统提示", null);
	}
}

MessageFormHandler.findReply = function() {
    if (!MiniuiTools.hasPurview("member_messages_reply")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	
	var rowsData = mini.get("messageGrid").getSelecteds();
	if (rowsData.length == 1) {
        window.location.href="replyMessageList.jsp?bumId="+rowsData[0].bumId;
	}
};


MessageFormHandler.replyMessage = function() {
    if (!MiniuiTools.hasPurview("member_messages_reply")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	
	var rowsData = mini.get("messageGrid").getSelecteds();
	if (rowsData.length == 1) {
		miniWin.afreshNewWin();
		miniWin.setOption("bodyStyle","padding:0px;");
		miniWin.setParams(650, 370, "回复留言");
		miniWin.load("messageFormTag.jsp", function() {
			var iframe = miniWin.getIFrameEl();
			iframe.contentWindow.MessageFormHandler.setDataTab(rowsData[0]);
		}, function(action) {
			new mini.get("messageGrid").reload();
		});

		miniWin.show();
	} else if (rowsData.length > 1) {
		mini.alert("不能同时编辑多条资源！", "系统提示", null);
	} else {
		mini.alert("请选择要编辑的资源！", "系统提示", null);
	}
};

MessageFormHandler.setDataTab = function(json) {
	$("#bumTime").html(changeDate(json.bumTime,3));
	$("#bumName").html(json.bumName);
	$("#bumTitle").html(json.bumTitle);
	
	if (parseInt(json.bumType) == 0) {
		$("#bumType").html("留言");
	}else if (parseInt(json.bumType) == 1) {
		$("#bumType").html("投诉");
	}else if (parseInt(json.bumType) == 2) {
		$("#bumType").html("询问");
	}else if (parseInt(json.bumType) == 3) {
		$("#bumType").html("售后");
	}else if (parseInt(json.bumType) == 4) {
		$("#bumType").html("求购");
	}
	
	$("#bumId").val(json.bumId);
	
	if (parseInt(json.bumState) == 0) {
		$("#bumState").html("隐藏");
	} else if (parseInt(json.bumState) == 1) {
		$("#bumState").html("展示");
	}
	
	$("#bumIp").html(json.bumIp);
	$("#bumContent").html(json.bumContent);
}

MessageFormHandler.saveReply = function() {
	if (editor.html().length > 0) {
		$.ajax( {
			url : "../../../savereply.do",
			type : "POST",
			data : {
				bumId : $("#bumId").val(),
				reply : editor.html(),
				lslUser: gbOperateUser,
    			lslAddress: gbOperateIp
			},
			cache : false,
			success : function(text) {
			MiniuiTools.miniInfo("系统提示","操作成功！", function(){
       			MessageFormHandler.closeCurrentWin("reply");
       		});
		},
		error : function(jqXHR, textStatus, errorThrown) {
			mini.alert(jqXHR.responseText, "系统提示", null);
			MessageFormHandler.closeCurrentWin();
		}
		});
	}
}
try {
	var editor = KindEditor.create('#' + editorId, {
		resizeType : 1,
		uploadJson : "../../resource/kindeditor/jsp/upload_json.jsp?imgDir="+ __msgpath,
		fileManagerJson : '../../resource/kindeditor/jsp/file_manager_json.jsp',
		allowPreviewEmoticons : false,
		allowImageUpload : true,
		items : [ 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor',
				'bold', 'italic', 'underline', 'removeformat', '|',
				'justifyleft', 'justifycenter', 'justifyright',
				'insertorderedlist', 'insertunorderedlist', '|', 'emoticons',
				'image', 'link' ]
	});
} catch (e) {
}

function changeDateToString(e) {
	return changeDate(e.row.bumTime,3);
};

function onbumTypeRender(e){
	var x = ["留言","投诉","询问","售后","求购"];
	var z;
	try {
		z = parseInt(e.row.bumType);
	} catch (e) {
		return "";
	}
	if (z >= 0 && z <= 1)
		return x[z];
	else
		return "";
}

/**
 * 关闭窗口
 * 
 * @param {}
 */
MessageFormHandler.closeCurrentWin = function(action) {
	if (window.CloseOwnerWindow)
		window.CloseOwnerWindow(action);
	else
		window.close();
}

MessageFormHandler.closeMiniWin = function() {
	MessageFormHandler.closeCurrentWin("cancel");
}

/**
 * 刷新页面数据及布局
 */
MessageFormHandler.afresh = function(){
	mini.get("messageGrid").reload();
	mini.get("messagePanel").doLayout();
}
